package com.shenglue.ssi.action;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.shenglue.ssi.domain.BuyInDetail;
import com.shenglue.ssi.domain.BuyInMain;
import com.shenglue.ssi.domain.Good;
import com.shenglue.ssi.service.BuyInDetailService;
import com.shenglue.ssi.service.BuyInMainService;
import com.shenglue.ssi.service.GoodService;

public class BuyInDetailAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private BuyInMain buyInMain;		// 主表记录的 Bean
	private BuyInDetail buyInDetail;	// 对应的 Bean
	private int buyInMainId;			// 所属的 主表记录 之Id
	private int buyInDetailId;			// 删除/修改 的 记录之Id
	private BuyInMainService   buyInMainService;   // 依赖的 Service（辅）
	private GoodService		   goodService;   	   // 依赖的 Service（辅）
	private BuyInDetailService buyInDetailService; // 依赖的 Service（主）

	/**
	 * 查询所有入库单子表信息方法
	 */
	public String queryAll() throws Exception {
		List<BuyInDetail> list = buyInDetailService.selectAllBuyInDetail( buyInMainId );
		if( list == null ) {
			// 其实不用创建，后面的JSP 够聪明  list = new ArrayList<BuyInDetail>();
			// 不能认为是错误，因为一开始是没有明细的  return INPUT;
		}

		buyInMain = buyInMainService.getOneBuyInMainForUpdate( buyInMainId );
		if( buyInMain == null ) {
			return INPUT;
		}

		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute( "buyInDetailList", list );
		request.setAttribute( "buyInMain", buyInMain );

		return SUCCESS;
	}

	/**
	 * 增加入库单子表记录方法
	 */
	public String addNew() throws Exception {
		boolean flag = buyInDetailService.saveBuyInDetail( buyInDetail );
		if(flag==true){
			return SUCCESS;
		}else{
			// 这2句 ，无法传递给 JSP，只能另想它法
			// HttpServletRequest request = ServletActionContext.getRequest();
			// request.setAttribute( "error", "增加明细信息失败，请检查商品信息是否重复" );

			errMsg = "增加明细信息失败，请检查商品信息是否重复";
			// 此句移到 errMsg 的 get 方法中了 ：errMsg = java.net.URLEncoder.encode( errMsg, "utf-8" );
			return ERROR;
		}
	}

	/**
	 * 删除入库单子表记录方法
	 */
	public String delete() throws Exception {

		boolean flag = buyInDetailService.deleteBuyInDetail( buyInDetailId );
		if( flag == true ) {
			return SUCCESS;
		}else{
			return ERROR;
		}
	}

	/**
	 * 修改入库单子表记录方法
	 */
	public String update() throws Exception {

		boolean flag = buyInDetailService.updateBuyInDetail( buyInDetail );
		if( flag == true ) {
			return SUCCESS;
		}else{
			// errMsg = "修改明细信息失败，试一下能否显示出来——能显示";
			return ERROR;
		}

	}

	/**
	 * 准备 转向 新增 入库单子表记录 的界面
	 */
	public String prepareAddBuyInDetail() {
		boolean flag = selectGoodOf1Supp();
		if( flag == true ) {
			return SUCCESS;
		}else{
			return ERROR;
		}
	}

	/**
	 * 准备 转向 修改 入库单子表记录 的界面
	 */
	public String prepareModBuyInDetail() {
		// 应先执行 这一段 取得 buyInDetail， 以 顺便获得 buyInMainId。因为 调本方法 的 url 不传递 buyInMainId 进来
		buyInDetail = buyInDetailService.getOneBuyInDetailForUpdate( buyInDetailId );
		if( buyInDetail == null ) {
			return ERROR;
		}

		// 由于不可修改 商品，所以下面的 查 Good 的代码 不需要了
		// buyInMainId = buyInDetail.getBuyInId(); // 然后才能调  selectGoodOf1Supp()

		// boolean flag = selectGoodOf1Supp();
		// if( flag == false ) {
		// 	return ERROR;
		// }

		return SUCCESS;
	}

	/**
	 * 预先查好 商品，为了制作下拉列表框   —— 当前入库单 有且仅有一个供应商，查该供应商的 所有商品
	 */
	private boolean selectGoodOf1Supp() {

		// 先从 buyInMainId 取得 buyInMain实例，再从中取得 suppId
		buyInMain = buyInMainService.getOneBuyInMainForUpdate( buyInMainId );
		if( buyInMain == null ) {
			return false;
		}
		int suppId = buyInMain.getSuppId();

		// 根据取得的 suppId，得到 currSuppAllGood，置于 request
		boolean bRtn;
		List<Good> currSuppAllGood = goodService.selectGoodOf1Supp( suppId );
		if( currSuppAllGood != null ){
			HttpServletRequest request = ServletActionContext.getRequest();
			request.setAttribute( "currSuppAllGood", currSuppAllGood );
			bRtn = true;
		}else{
			bRtn = false;
		}
		return bRtn;
	}

	public BuyInDetailAction() {
	}

	public BuyInMain getBuyInMain() {
		return buyInMain;
	}

	public void setBuyInMain(BuyInMain buyInMain) {
		this.buyInMain = buyInMain;
	}

	public BuyInDetail getBuyInDetail() {
		return buyInDetail;
	}

	public void setBuyInDetail(BuyInDetail buyInDetail) {
		this.buyInDetail = buyInDetail;
	}

	public int getBuyInMainId() {
		return buyInMainId;
	}

	public void setBuyInMainId(int buyInMainId) {
		this.buyInMainId = buyInMainId;
	}

	public int getBuyInDetailId() {
		return buyInDetailId;
	}

	public void setBuyInDetailId(int buyInDetailId) {
		this.buyInDetailId = buyInDetailId;
	}


	public BuyInMainService getBuyInMainService() {
		return buyInMainService;
	}

	public void setBuyInMainService(BuyInMainService buyInMainService) {
		this.buyInMainService = buyInMainService;
	}

	public GoodService getGoodService() {
		return goodService;
	}

	public void setGoodService(GoodService goodService) {
		this.goodService = goodService;
	}

	public BuyInDetailService getBuyInDetailService() {
		return buyInDetailService;
	}

	public void setBuyInDetailService(BuyInDetailService buyInDetailService) {
		this.buyInDetailService = buyInDetailService;
	}

}
